(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.prodtype.viewController', viewController);
	viewController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'prodtypeService', 'modalService', '$log' ];
	function viewController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, prodtypeService, modalService, $log) {
		var ctrl = this, prodtypeInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.prodtype = {};
		ctrl.init = function() {	
			prodtypeInfo.prodTypeType = 'G';
			ctrl.innerGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.interet.tableConfigs.table);
			ctrl.innerGridOptions.data = 'vm.prodtype.intList';
			ctrl.innerGridOptions.enableRowHeaderSelection = false;
			
			ctrl.innerEventGridOptions = gridService.createInnerGridOptions($scope, ctrl.layoutConfig.formConfigs.event.tableConfigs.table, rowSelectionChanged);
			ctrl.innerEventGridOptions.data = 'vm.prodtype.events';
			ctrl.innerEventGridOptions.multiSelect= false;
			return authService.view(prodtypeInfo).then(function(response) {
				ctrl.prodtype = response.data || prodtypeInfo;
				ctrl.prodtype.events = ctrl.prodtype.events ||[];
			}, angular.noop);
		};
		
		ctrl.viewEvent = function() {
			$log.debug('view event of prodtype events');
			if (!ctrl.selectedEventRows || ctrl.selectedEventRows.length == 0) {
				modalService.openErrorDialogs({
					messages : [ 'title.dialogs.info.norecsel' ]
				});
				return;
			}
			$log.debug('open custom dialog');
			modalService.openCustomDialogs({
			   'dialogsModule' : 'cda.prodtype.event.view',
			   'params':{
				   'selectedRows' : ctrl.selectedEventRows
			    }
			});			
		};
		
		function rowSelectionChanged(eventRow, selectedRows) {
			$log.debug('row selected:' + selectedRows);
			ctrl.selectedEventRows = selectedRows;
		}		
	}
})(angular);